package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.SimplePerson;
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @date: 2022-08-02 16:37
 * @author: ztkj-hzb
 * @description: tableview  焦点 属性设置
 **/
public class FocusSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        Button button = new Button("锁定焦点");

        TableView<SimplePerson> tableView = new TableView<>();

        //设置宽高
        tableView.setPrefSize(600, 400);

        //设置数据
        ObservableList<SimplePerson> persons = FXCollections.observableArrayList();

        persons.add(new SimplePerson("小天", 20, "湖北省", false));
        persons.add(new SimplePerson("小地", 30, "湖南省", true));
        persons.add(new SimplePerson("小玄", 40, "广东省", false));
        persons.add(new SimplePerson("小黄", 50, "浙江省", true));
        tableView.setItems(persons);

        //创建列
        TableColumn<SimplePerson, String> nameTableCol = new TableColumn<>("用户名");
        TableColumn<SimplePerson, Number> ageTableCol = new TableColumn<>("年龄");
        TableColumn<SimplePerson, String> addressTableCol = new TableColumn<>("地址");
        TableColumn<SimplePerson, Boolean> marriedTableCol = new TableColumn<>("是否已婚");
        tableView.getColumns().addAll(nameTableCol, ageTableCol, addressTableCol, marriedTableCol);

        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        ageTableCol.setCellValueFactory(new PropertyValueFactory<>("age"));
        addressTableCol.setCellValueFactory(new PropertyValueFactory<>("address"));
        marriedTableCol.setCellValueFactory(new PropertyValueFactory<>("married"));

        AnchorPane.setTopAnchor(tableView, 50.0);
        anchorPane.getChildren().addAll(button, tableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

        //调整单元格宽度
        DoubleBinding divide = tableView.widthProperty().divide(tableView.getVisibleLeafColumns().size());
        for (TableColumn<SimplePerson, ?> visibleLeafColumn : tableView.getVisibleLeafColumns()) {
            visibleLeafColumn.prefWidthProperty().bind(divide.subtract(1));
        }

        tableView.getSelectionModel().setCellSelectionEnabled(true);

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                //设置光标选择指定单元格
                tableView.getFocusModel().focus(new TablePosition(tableView, 2, ageTableCol));
                //设置光标选择上一个单元格
                tableView.getFocusModel().focusAboveCell();
                //设置光标选择下一个单元格
                tableView.getFocusModel().focusBelowCell();
                //设置光标选择坐标单元格
                tableView.getFocusModel().focusLeftCell();
                //设置光标选择右边单元格
                tableView.getFocusModel().focusRightCell();

                tableView.requestFocus();
            }
        });


    }
}
